package com.doge.walknovel.core.base.adapters

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.doge.wnpro.R
import com.doge.walknovel.utils.inflate

abstract class BaseAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var listener: AdapterCallback? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            ItemType.ErrorPlaceHolder -> ErrorPlaceHolderVH(parent.inflate(R.layout.layout_recycler_error))
            ItemType.EmptyPlaceHolder -> EmptyPlaceHolderVH(parent.inflate(R.layout.layout_recycler_empty))
            ItemType.LoadingPlaceHolder -> MoreLoadingVH(parent.inflate(R.layout.layout_recycler_loading))
            else -> UnknownVH(View(parent.context).apply {
                layoutParams = ViewGroup.LayoutParams(0, 0)
            })
        }
    }

    fun setListener(callback: AdapterCallback?) {
        listener = callback
    }

    protected val callback: AdapterCallback = { item, action ->
        listener?.invoke(item, action)
    }
}